/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.mpd.musicjukebox.ui.components;

import com.bff.bjj.ui.component.MusicListCellRenderer;
import com.bff.mpd.musicjukebox.lib.ColorProperties;
import java.awt.Component;
import java.util.HashMap;
import javax.swing.JList;
import org.bff.javampd.objects.MPDGenre;

/**
 *
 * @author bfindeisen
 */
public class GenreListCellRenderer extends MusicListCellRenderer {

    private static HashMap<MPDGenre, GenreCellPanel> genrePanels;

    public GenreListCellRenderer() {
        super();
        genrePanels = new HashMap<MPDGenre, GenreCellPanel>();
    }

    @Override
    public Component getListCellRendererComponent(JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean isFocused) {

        GenreCellPanel panel = null;

        if (value instanceof MPDGenre) {
            MPDGenre genre = (MPDGenre) value;
            panel = genrePanels.get(genre);
            if (panel == null) {
                panel = new GenreCellPanel(genre);
                genrePanels.put(genre, panel);
            }
        } else {
            return super.getListCellRendererComponent(list, value, index, isSelected, isFocused);
        }
        if (panel != null) {
            if ((index % 2) == 0) {
                panel.setEven();
            } else {
                panel.setOdd();
            }

            if (!isSelected) {
                panel.setForeground(ColorProperties.LIST_FOREGROUND);
                panel.setNotSelected();
            } else {
                panel.setSelected();
            }

            if (panel.getPreferredSize().getHeight() > 1) {
                list.setFixedCellHeight((int) panel.getPreferredSize().getHeight());
            }

        }

        setColors(list, index, isSelected);


        if (panel != null) {
            return panel;
        } else {
            setText(value.toString());
            return this;
        }
    }
}
